import NetcallWeixin from './NIM_Web_Netcall_weixin_v6.2.0'
import SDK from './NIM_Web_SDK_weixin_v6.2.0'
import Emitter from './emitter.js'
let app = getApp()
export default class NetcallController {
    constructor(props) {
        SDK.NIM.use(NetcallWeixin)
        NetcallWeixin.destroy()
        this.netcall = app.globalData.netcall = NetcallWeixin.getInstance(props)
        this.bindNetcallEvent()
        app.globalData.emitter = new Emitter()
    }
    bindNetcallEvent() {
        app.globalData.netcall.on('syncDone', (data) => {
            console.log(new Date())
            console.log('登录成功', data)
            app.globalData.emitter.emit('syncDone', data)
        })
        this.netcall.on('clientLeave', (data) => {
            console.log('有人离开了', data)
            app.globalData.emitter.emit('clientLeave', data)
        })
        this.netcall.on('clientJoin', (data) => {
            console.log(new Date())
            // {account,cid,mode,uid,url}
            console.log('有人加入了0', data)
            app.globalData.emitter.emit('clientJoin', data)
        })
        this.netcall.on('callRejected', (data) => {
            console.log('对方拒绝了', data)
            app.globalData.emitter.emit('callRejected', data)
        })
        this.netcall.on('callerAckSync', (data) => {
            // {timetag,type,fromClientType:number,cid,accepted}
            console.log('callerAckSync: 其他端已经处理了', data)
            wx.showToast({
                title: '已在其他端处理！',
                duration: 2000,
                icon: 'none',
                success: function () {
                    setTimeout(() => {
                        let pages = getCurrentPages()
                        let currentPage = pages[pages.length - 1]
                        if (currentPage.route.includes('videoCall') || currentPage.route.includes('videoCallMeeting')) {
                            wx.navigateBack(1)
                        }
                    }, 2000)
                }
            })
        })
        this.netcall.on('hangup', (data) => {
            console.log('hangup', data)
            app.globalData.emitter.emit('hangup', data)
        })
        this.netcall.on('control', (data) => {
            console.log('control', data)
            // {cid,account,command}
            app.globalData.emitter.emit('control', data)
        })
        this.netcall.on('joinChannel', (data) => {
            // {uid,account,cid}
            console.log('joinChannel', data)
            app.globalData.emitter.emit('joinChannel', data)
        })
        this.netcall.on('clientUpdate', (data) => {
            console.log('有人更新了', data)
        })
        this.netcall.on('error', (error) => {
            console.error('出错了', error)
        })
        this.netcall.on('open', (data) => {
            console.log('socket建立成功', data)
        })
        this.netcall.on('willreconnect', (data) => {
            // 直播通道准备重连
            app.globalData.emitter.emit('willreconnect', data)
        })
        this.netcall.on('sendCommandOverTime', (data) => {
            console.log('发送命令超时', data)
        })
        this.netcall.on('liveRoomClose', (data) => {
            console.log('互动直播房间解散了', data)
        })
        this.netcall.on('sessionDuration', (data) => {
            console.log('===结束通话(ms)：', data)
        })
    }
}